//
//  JokeViewController.m
//  TrekkingTravel
//
//  Created by dlios on 15/8/20.
//  Copyright (c) 2015年 TrekkingTravel. All rights reserved.
//

#import "JokeViewController.h"
#import "Constants.h"
//#import "WidthAndHeight.pch"
//#import "HTTPTOOL.h"
#import <MJRefresh.h>
#import "JokeModel.h"
#import "UMSocial.h"


@interface JokeViewController ()

@property (nonatomic, retain) UIImageView *image;

@end

@implementation JokeViewController

- (void)dealloc
{
    [_HUD release];
    [_maxtime release];
    [_jokeArray release];
    [_collectionView release];
    [_image release];
    [super dealloc];
}

// 解析数据
- (void)getData:(NSString *)maxtime
{
    //添加菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"加载中...";
    [self.HUD show:YES];
    
    NSString *url = [NSString stringWithFormat:@HappyJokeUrl,maxtime];
        
    [HTTPTOOL GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
            
            if (self.isUpLoading == NO) {
                [self.jokeArray removeAllObjects];
                
            }
            
            NSMutableDictionary *dic = result;
            NSMutableDictionary *dic1 = dic[@"info"];
            self.maxtime = dic1[@"maxtime"];
            //        NSLog(@"%@", self.maxtime);
            NSMutableArray *array = dic[@"list"];
            for (NSMutableDictionary *dic2 in array) {
                JokeModel *jokeM = [[JokeModel alloc] init];
                [jokeM initWithDic:dic2];
                [self.jokeArray addObject:jokeM];
            }
            [self.collectionView.header endRefreshing];
            [self.collectionView.footer endRefreshing];
            [self.collectionView reloadData];
            [self.HUD removeFromSuperview];

        } fail:^(NSError *error) {
            
            [self.collectionView.header endRefreshing];
            [self.collectionView.footer endRefreshing];
            [self.collectionView reloadData];
            [self.HUD removeFromSuperview];
        }];
    
            
//            ViewController *helpVC = [[ViewController alloc] init];
//            UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:helpVC];
//            [helpVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
//            [self presentViewController:naVC animated:YES completion:^{
//                
//                
//            }];
//            [naVC release];
//            [helpVC release];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"摘自不得姐";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.image = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.image.image = [UIImage imageNamed:@"Back"];
    [self.view addSubview:self.image];
    self.image.alpha = 0.7;
    [_image release];
    
    //刷新所需变量
    self.maxtime = @"";
    self.jokeArray = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, VCWIDTH, VCHEIGH - 113 * HEIGH) collectionViewLayout:flowLayout];
    //设置item的大小
    flowLayout.itemSize = CGSizeMake(VCWIDTH - 20, VCHEIGH);
    //设置每行item的间隔
    flowLayout.minimumLineSpacing = 20;
    //设置每列item的间隔
    flowLayout.minimumInteritemSpacing = 10;
    //设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置item各边的距离(上左下右)
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    //注册cell
    [self.collectionView registerClass:[JokeCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //刷新
    [self addHeader];
    [self addFooter];
    
   
    //请求网络数据
    [self getData:@""];
  
    
}
// 刷新
- (void)addHeader
{
    [self.collectionView.header beginRefreshing];
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.maxtime = @"";
        self.isUpLoading = NO;//标记为下拉操作
        [self getData:self.maxtime];//重新请求数
        
    }];

}

// 加载
- (void)addFooter
{
    
    [self.collectionView.footer beginRefreshing];
    self.collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        // 进入刷新状态就会回调这个Block
        self.isUpLoading = YES; //标记为上拉操作
        [self getData:self.maxtime];//请求更多数据
    }];
    [self.HUD hide:YES afterDelay:3];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// cell必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.jokeArray.count;
}

// 布局item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从系统的重用池中取item
    JokeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor clearColor];
    cell.jokeM = [self.jokeArray objectAtIndex:indexPath.row];

    return cell;
                                                                              
}

//分享
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55a3a50767e58eded90035da"
                                      shareText:[[self.jokeArray objectAtIndex:indexPath.row] text]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToTencent,nil]
                                       delegate:nil];
}

//item的自适应
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JokeModel *jokeM = [self.jokeArray objectAtIndex:indexPath.row];
    CGFloat height = [JokeCollectionViewCell heightForContent:jokeM.text];
    self.sizeForItem = CGSizeMake(VCWIDTH - 20, height + 10);
    return self.sizeForItem;
}

    
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
